﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Plat.WfProcess.Utility;
using Plat.WfProcess.DataModel;
using Plat.WfProcess.Business;
using Plat.WfProcess.Common;

namespace Plat.WfProcess.Engine
{
    /// <summary>
    /// 节点的基类
    /// </summary>
    public abstract class NodeBase
    {
        #region 属性和构造函数
        /// <summary>
        /// 节点定义属性
        /// </summary>
        public ActivityEntity Activity
        {
            get;
            set;
        }
        
        /// <summary>
        /// 节点实例
        /// </summary>
        public ActivityInstanceEntity ActivityInstance
        {
            get;
            set;
        }

        /// <summary>
        /// 活动节点管理类
        /// </summary>
        internal ActivityManager activityManager;
        internal ActivityManager ActivityManager
        {
            get
            {
                if (activityManager == null)
                {
                    activityManager = new ActivityManager(Activity.ProcessGUID);
                }
                return activityManager;
            }
        }

        /// <summary>
        /// 活动节点实例管理对象
        /// </summary>
        internal ActivityInstanceManager activityInstanceManager;
        internal ActivityInstanceManager ActivityInstanceManager
        {
            get
            {
                if (activityInstanceManager == null)
                {
                    activityInstanceManager = new ActivityInstanceManager();
                }
                return activityInstanceManager;
            }
        }
        

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="currentActivity"></param>
        public NodeBase(ActivityEntity currentActivity)
        {
            Activity = currentActivity;
        }
        #endregion



        
        #region 创建、插入和完成
        /// <summary>
        /// 创建节点对象
        /// </summary>
        /// <param name="processInstance">流程实例</param>
        internal void CreateActivityInstanceObject(ProcessInstanceEntity processInstance)
        {
            ActivityInstanceManager aim = new ActivityInstanceManager();
            ActivityInstance = aim.CreateActivityInstanceObject(Activity, processInstance);

            AfterActivityInstanceObjectCreated();
        }

        internal virtual void AfterActivityInstanceObjectCreated()
        {

        }



        /// <summary>
        /// 插入实例数据
        /// </summary>
        /// <param name="activityInstance"></param>
        /// <param name="wfLinqDataContext"></param>
        internal virtual string InsertActivityInstance(ActivityInstanceEntity activityInstance,
            ProcessModelDataContext wfLinqDataContext)
        {
            return ActivityInstanceManager.Insert(activityInstance, wfLinqDataContext);
        }

        /// <summary>
        /// 插入连线实例的方法
        /// </summary>
        /// <param name="processInstance"></param>
        /// <param name="fromToTransition"></param>
        /// <param name="fromActivityInstance"></param>
        /// <param name="toActivityInstance"></param>
        /// <param name="conditionParseResult"></param>
        /// <param name="wfLinqDataContext"></param>
        /// <returns></returns>
        internal virtual string InsertTransitionInstance(ProcessInstanceEntity processInstance,
            TransitionEntity fromToTransition,
            ActivityInstanceEntity fromActivityInstance,
            ActivityInstanceEntity toActivityInstance,
            ProcessModelDataContext wfLinqDataContext)
        {
            TransitionInstanceManager tim = new TransitionInstanceManager();
            TransitionInstanceEntity transitionInstanceObject = tim.CreateTransitionInstanceObject(processInstance,
                fromToTransition,
                fromActivityInstance.ActivityInstanceGUID,
                toActivityInstance.ActivityInstanceGUID,
                (byte)ConditionParseResultEnum.Passed);

            return tim.Insert(transitionInstanceObject,
                wfLinqDataContext);
        }

        /// <summary>
        /// 节点对象的完成方法
        /// </summary>
        /// <param name="activityInstanceGUID"></param>
        /// <param name="activityResource"></param>
        /// <param name="wfLinqDataContext"></param>
        internal virtual void CompleteActivityInstance(string activityInstanceGUID,
            ActivityResource activityResource,
            ProcessModelDataContext wfLinqDataContext)
        {
            //设置完成状态
            ActivityInstanceManager.Complete(activityInstanceGUID,
                activityResource.CurrentLogonUser,
                wfLinqDataContext);
        }

        /// <summary>
        /// 同步内存活动实例的状态
        /// </summary>
        /// <param name="state"></param>
        internal void SyncActivityInstanceObjectState(NodeStateEnum state)
        {
            this.ActivityInstance.State = (short)state;
        }

        /// <summary>
        /// 获得运行节点需要的Tokens数目
        /// </summary>
        /// <returns></returns>
        internal int GetTokensRequired()
        {
            TransitionManager tm = new TransitionManager(Activity.ProcessGUID);
            int tokensRequired = tm.GetBackwardTransitionListNecessaryCount(Activity.ActivityGUID);
            return tokensRequired;
        }
               

        #endregion
    }
}
